/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is COID http service.
 *
 * The Initial Developer of the Original Code is
 * PosAm.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Brano Kemen
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
 
#ifndef __COID_SERVICES_WEBTALK__HEADER_FILE__
#define __COID_SERVICES_WEBTALK__HEADER_FILE__


#include "coid/svc/coidsvc.h"
#include "smiley.h"

class WebtalkCore;


COID_NAMESPACE_BEGIN

////////////////////////////////////////////////////////////////////////////////
class Webtalk
{
    //NET_OPTIONS: nolog_system_calls
    //NET_RELEASE: 0.1
public:

    enum { SESSION_TIMEOUT = 60 };

    Webtalk() { _wc = 0; }

    net_fnc_s net_meta opcd accept_connect( net_in const charstr& path, net_in const charstr& name, net_in const password& pwd, net_out uint* pos );

    net_fnc_s opcd accept_connect_shared()
    {
        return ersDENIED;
    }

    net_fnc_s opcd accept_startup_params( cmd_interface& cmd )
    {
        _cmdin = &cmd;
        _cmdin->request_autodestroy( SESSION_TIMEOUT );
        return 0;
    }

    net_fnc_s net_meta opcd accept_destroy();


    net_fnc net_meta opcd read( net_inout uint& ref, net_out dynarray<token>& rows, net_out uint* flg );
    net_fnc net_meta opcd send( charstr& text );

    net_fnc net_meta opcd userlist( net_out charstr& usrs );

    net_fnc net_meta opcd smiles( net_fetch const dynarray<Smiley>*& smi );


protected:

    static bool is_alphanum(char c)
    { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }

    static charstr& append_time( charstr& buf );

    opcd send_sysmsg( charstr& text );

    static void insert_fmt( charstr& dst, char fc, token& text, uint i )
    {
        if( fc == '*' )  dst += "<b>";  else  dst += "<i>";
        dst.add_from( text.ptr(), i );
        if( fc == '*' )  dst += "</b>";  else  dst += "</i>";
        text.shift_start(i+1);
    }

    void check_html( token text, charstr& dst );
    bool check_smiley( token& text, charstr& dst );
    static void make_href( token& text, charstr& dst );
    static uints skip_nested_tags( const token& text );

protected:

    cmd_interface* _cmdin;
    uint _id;
    charstr _name;
    charstr _buf;

    WebtalkCore* _wc;

    uint _usrchng;
};

COID_NAMESPACE_END

#endif //__COID_SERVICES_WEBTALK__HEADER_FILE__
